from docx import Document
from docx.shared import Inches

#srcObj=[{'title':{'style':'Heading 1','bold':True,'italic':True,'level':[0-9]}},{'article':{'bold':True,'style':'normal','text':' and some ','italic':True}}]
srcObj=[{'style':'Heading 1','Font':'黑体','FontSize':'小二','bold':'bold','italic':'italic','Alignment':'Alignment','SymbolType':'SymbolType'}]

def writeDocx(document,srcObj):
    paragraphNowIndex = -1
    for each in srcObj:
        while each['num'] > paragraphNowIndex:
            res = document.add_paragraph(style=each['paragraph']['style'])
            paragraphNowIndex += 1
        run = res.add_run(each['text'])
        # run.style = each['style']
        # font = document.styles[each['paragraph']['style']].font
        # run.font.name = font.name
        #res.FontSize = each['FontSize']
        # run.bold = each.get('bold')
        # run.italic = each.get('italic')
        # run.underline = each.get('underline')
        # res.Alignment = each['Alignment']
        #res.SymbolType = each['SymbolType']
    return document

def main():
    document = Document()
    writeDocx(document,srcObj)
    
if __name__ == '__main__':
    main()

'''def add_title():
    res = add_run()
    if  = :
        title = document.add_heading(srcObj['title']['text'], level= )
    if  = :
        title.bold = srcObj['title']['bold']
    elif  = :
        title.italic=srcObj['title']['italic']
    
    

def add_article():
    p = document.add_paragraph()
    article = srcObj['article'][0]
    p.add_run('bold').bold = article['bold']
    #p.add_run('bold').bold = True
    p.add_run(article['text'])
    #p.add_run('italic.').italic = srcObj['italic']
    p.add_run('italic.').italic = article['italic']
    p.add_run('text with emphasis','Emphasis')
    
add_title()
add_article()'''

'''document.add_heading('Document Title', 0)

p = document.add_paragraph('A plain paragraph having some ')
p.add_run('bold').bold = True
p.add_run(' and some ')
p.add_run('italic.').italic = True

document.add_heading('Heading, level 1', level=1)
document.add_paragraph('Intense quote', style='Intense Quote')

document.add_paragraph(
    'first item in unordered list', style='List Bullet'
)
document.add_paragraph(
    'first item in ordered list', style='List Number'
)


#document.add_picture('monty-truth.png', width=Inches(1.25))
document.add_paragraph('Normal text')
#p.add_run('text with emphasis','Emphasis')

records = (
    (3, '101', 'Spam'),
    (7, '422', 'Eggs'),
    (4, '631', 'Spam, spam, eggs, and spam')
)

table = document.add_table(rows=1, cols=3)
hdr_cells = table.rows[0].cells
hdr_cells[0].text = 'Qty'
hdr_cells[1].text = 'Id'
hdr_cells[2].text = 'Desc'
for qty, id, desc in records:
    row_cells = table.add_row().cells
    row_cells[0].text = str(qty)
    row_cells[1].text = id
    row_cells[2].text = desc
'''

#document.add_page_break()

#document.save('demo.docx')
